package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.mapper.CollectMapper;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper contentMapper;
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        //判断是否有序号,如果没有就设置最大的序号+1
        if (strategyDetail.getSequence() == null) {
            int max = strategyDetailMapper.selectMaxSequence(strategyDetail.getCatalog().getId());
            strategyDetail.setSequence(max + 1);
        }

        //如果是发布状态就需要设置发布时间
        if (strategyDetail.getState()) {
            strategyDetail.setReleaseTime(new Date());
        }

        if (strategyDetail.getId() != null) {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);

            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            contentMapper.updateByPrimaryKey(strategyContent);

        } else {
            strategyDetailMapper.insert(strategyDetail);

            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            contentMapper.insert(strategyContent);
        }
    }

    @Override
    public StrategyContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyDetail getById(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
      /*  for (StrategyDetail strategyDetail : list) {
            //
            Collect c = collectMapper.get2(UserContext.getCurrentUser().getId(), strategyDetail.getId());
            //如果进入页面的时候用这个用户的Id和这篇文章的id去查收藏表中是否有这个数据，如果有就给文章对象中的collect字段赋值
            //一个true，告知页面此篇文章已被收藏，按需回显
            if (c != null){
                strategyDetail.setCollect(true);
            }else {
                strategyDetail.setCollect(false);
            }
        }*/
        return new PageInfo<>(list);
    }

}
